// pages/orderView/orderView.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderData: [],
    activeTab: 'all',
    orderStatus: '',
    BASE_URL: "http://127.0.0.1:9100/api",
    userId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const app = getApp();
    this.setData({
      userId: app.globalData.userId
    });
    this.getOrderList();
  },

  // 返回方法
  goBack() {
    wx.navigateBack({
      delta: 1 // 返回上一级页面
    });
  },


  // 定义切换标签
  onTabChange(e) {
    const tab = e.currentTarget.dataset.tab;
    if (tab === 'all') {
      this.setData({
        orderStatus: '',
        activeTab: 'all'
      });
    } else if (tab === 'pending') {
      this.setData({
        orderStatus: '1',
        activeTab: 'pending'
      });
    } else if (tab === 'paid') {
      this.setData({
        orderStatus: '2',
        activeTab: 'paid'
      });
    }
    this.getOrderList();
  },
  getOrderList() {
    const { userId, orderStatus, BASE_URL } = this.data;
    wx.request({
      url: `${BASE_URL}/order/userOrderList?userId=${userId}&status=${orderStatus}`,
      method: 'GET',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: (res) => {
        if (res.data.code === 200) {
          const orderData = res.data.data.map(item => {
            if (item.cover) {
              item.cover = `${BASE_URL}/staticfiles/image/${item.cover}`;
            }
            return item;
          });
          this.setData({
            orderData
          });
        }
      },
      fail: (err) => {
        console.log(err.errMsg);
      }
    });
  },
  // 跳转景区详情
  handleDetail(e) {
    const thingId = e.currentTarget.dataset.thingId;
    wx.navigateTo({
      url: `/pages/thingDetail/thingDetail?id=${thingId}`
    });
  },
  // 取消订单
  handleCancel(e) {
    const app = getApp();
    const token = app.globalData.token;
    const item = e.currentTarget.dataset.item;
    const { BASE_URL } = this.data;
    wx.request({
      url: `${BASE_URL}/order/cancelUserOrder`,
      method: 'POST',
      data: {
        id: item.id
      },
      header: {
        'TOKEN': token,
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: (res) => {
        if (res.data.code === 200) {
          wx.showToast({
            title: '取消成功',
            icon: 'success'
          });
          this.getOrderList();
        } else {
          wx.showToast({
            title: '取消失败',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        console.log(err.errMsg);
        wx.showToast({
          title: '请求失败',
          icon: 'none'
        });
      }
    });
  },

  formatTime(timestamp) {
    const date = new Date(parseInt(timestamp));
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hour = String(date.getHours()).padStart(2, '0');
    const minute = String(date.getMinutes()).padStart(2, '0');
    return `${year}-${month}-${day} ${hour}:${minute}`;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})